package org.hxyjs.service.impl;

import org.hxyjs.dao.MongodbDao;
import org.hxyjs.dao.RedisDao;
import org.hxyjs.dao.RestaurantDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.*;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.RestaurantService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Stream;

@Service("restaurantService")
public class RestaurantServiceImpl implements RestaurantService , AccountService {
    @Resource
    RestaurantDao restaurantDao;
    @Resource
    RedisDao redisDao;
    @Resource
    MongodbDao mongodbDao;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Override
    public DTO<Restaurant> getAllRestaurants()  throws Exception{
        List<Restaurant> restaurants = restaurantDao.getAllRestaurants();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(restaurants).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurants);
    }

    @Override
    public DTO<Restaurant> deleteRestaurantByRno(String rno)  throws Exception{
        Integer result = restaurantDao.deleteRestaurantByRno(rno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Restaurant> updateRestaurantByRno(Restaurant restaurant) throws Exception {
        Integer result = restaurantDao.updateRestaurantByRno(restaurant);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Restaurant> addRestaurantByRno(Restaurant restaurant) throws Exception {
        Integer result = restaurantDao.addRestaurantByRno(restaurant);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage());
    }
    @Override
    public DTO<Restaurant> getRestaurantByRno(String rno)  throws Exception{
        Restaurant restaurants = restaurantDao.getRestaurantByRno(rno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(restaurants).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurants);
    }

    @Override
    public DTO<Restaurant> selectRestaurantWithorderAndUser(String rno) throws Exception {
        Restaurant restaurant = restaurantDao.selectRestaurantWithorderAndUser(rno);
        List<String> keys = redisDao.getValuesListByKeyRegexp(stringRedisTemplate, "*" + rno + "*");
        List<MongoDbCondition<String,String>> mongoDbConditions = new ArrayList<MongoDbCondition<String,String>>();
//        keys.forEach((key)->{
//            map.put("_id",key);
//        });
        //Stream流  iterate 逐个往下循环 迭代器 从0 开始每数一个i+1 limit极限 直到keys
        Stream.iterate(0, i -> i + 1).limit(keys.size()).forEach(i -> {
            String key= keys.get(i);
            mongoDbConditions.add(new MongoDbCondition<>("_id",key) );
        });

        List<MongoDBDocument> mongoDBDocuments = mongodbDao.getListByRegxp( mongoDbConditions, "OrderFormWithOrderItem");
        mongoDBDocuments.forEach(mongoDBDocument->{
            restaurant.getOrderForm().add((OrderForm) mongoDBDocument.getT());
            System.out.println(mongoDBDocument);
        });
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(rno).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurant);
    }

    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Restaurant> getRestaurantsByPage(PageInfo pageInfo, String cacheId) throws Exception {
        List<Restaurant> restaurants = restaurantDao.getRestaurantByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(restaurants).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurants,pageInfo);
    }

    @Override
    public DTO<Restaurant> getFreeRestaurantByDate(String beginDate, String endDate) throws Exception {
        List<Restaurant> restaurants = restaurantDao.getFreeRestaurantByDate( beginDate, endDate);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(restaurants).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurants);
    }

    @Override
    public DTO<Restaurant> getRestaurantsByPage(PageInfo pageInfo) throws Exception {
        List<Restaurant> restaurants = restaurantDao.getRestaurantByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(restaurants).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Restaurant>(messageEnum.getCode(),messageEnum.getMessage(),restaurants,pageInfo);
    }

    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        DTO<Account> dto = new DTO<Account>();
        Optional<Account> adminOP = Optional.ofNullable(restaurantDao.getAccountByAccountName(accountName));
        dto.setT(adminOP.get());
        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        dto.setCode(messageEnum.getCode());
        dto.setMsg(messageEnum.getMessage());
        return dto;
    }
}
